package com.paomi.client.activity.payMoney;

import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.detail.OrderDetailActivity;
import com.paomi.client.activity.pay.model.PaySureBean;
import com.paomi.client.activity.payResult.ResultPayActivity;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.SetBeanPay;
import com.paomi.client.net.Constants;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 支付订单页面
 */
public class PayMoneyActivity extends BaseMvpActivity<PayMoneyView, PayMoneyPresenter> {


    @BindView(R.id.chart_tv)
    TextView chartTv;
    @BindView(R.id.btn_we_chart)
    TextView btnWeChart;
    @BindView(R.id.ali_pay)
    TextView aliPay;
    @BindView(R.id.btn_aliPay)
    TextView btnAliPay;
    @BindView(R.id.btn_pay)
    Button btnPay;
    private PaySureBean.PaySureJson paySureJson;
    private int getType = 0;//type支付类型 1 微信 2 支付宝
    private String goodId;//商品id
    @BindView(R.id.button_goback)
    TextView button_goback;

    @NonNull
    @Override
    public PayMoneyPresenter createPresenter() {
        return new PayMoneyPresenter();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.pay_title);
    }

    @Override
    protected void showBack(TextView button_goback) {
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay_money;
    }

    @Override
    protected void initEvent() {
        chartTv.setTypeface(iconfont);
        btnWeChart.setTypeface(iconfont);
        aliPay.setTypeface(iconfont);
        btnAliPay.setTypeface(iconfont);
        goodId = getIntent().getStringExtra("goodId");
        paySureJson = (PaySureBean.PaySureJson) getIntent().getSerializableExtra("paySure");

        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
        button_goback.setTypeface(iconfont);
        button_goback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backDialog();
            }
        });
        btnPay.setText(getResources().getString(R.string.going_pay)+"￥"+paySureJson.getPrice());
    }

    @OnClick({R.id.btn_we_chart, R.id.btn_aliPay, R.id.btn_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_we_chart://微信选中
                getType = 1;
                btnAliPay.setText(getResources().getString(R.string.uncheck_sex));
                btnAliPay.setTextColor(getResources().getColor(R.color.text_gray_99));
                btnWeChart.setText(getResources().getString(R.string.check_sex));
                btnWeChart.setTextColor(getResources().getColor(R.color.main_color));
                break;
            case R.id.btn_aliPay://支付宝选中
                getType = 2;
                btnWeChart.setText(getResources().getString(R.string.uncheck_sex));
                btnWeChart.setTextColor(getResources().getColor(R.color.text_gray_99));
                btnAliPay.setText(getResources().getString(R.string.check_sex));
                btnAliPay.setTextColor(getResources().getColor(R.color.main_color));
                break;
            case R.id.btn_pay://立即支付
                if (getType == 0) {
                    return;
                }
//                btnPay.getBackground().setAlpha(100);
                getPresenter().payIsCan(getType, "" + paySureJson.getOrderId(), this);
                break;
        }
    }

    private Dialog dialog;

    void backDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        dialog = new Dialog(this, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(getResources().getString(R.string.pay_question));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(getResources().getString(R.string.pay_question_msg));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setText(getResources().getString(R.string.pay_submit));
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setText(getResources().getString(R.string.pay_cancel));
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent i = new Intent(PayMoneyActivity.this, OrderDetailActivity.class);
                i.putExtra("orderId", ""+paySureJson.getOrderId());
                startActivity(i);
                finish();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (dialog == null || !dialog.isShowing()) {
                backDialog();
            } else {
                dialog.dismiss();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 微信支付的回调
     * @param result
     */
    @Subscribe
    public void onEvent(SetBeanPay result) {
        Log.e("支付的回调",result.getMessage());
        if (result.getMessage().equals("支付成功") && result.getCodeData()==Constants.wxSuccess) {
            getIntent(ResultPayActivity.class);
            finish();
        } else if (result.getMessage().equals("支付失败")&& result.getCodeData()==Constants.wxFailure) {
            Intent i = new Intent(this, OrderDetailActivity.class);
            i.putExtra("orderId", ""+paySureJson.getOrderId());
            startActivity(i);
            finish();

        } else if (result.getMessage().equals("支付取消")&& result.getCodeData()==Constants.wxCancel) {
            Intent i = new Intent(this, OrderDetailActivity.class);
            i.putExtra("orderId", ""+paySureJson.getOrderId());
            startActivity(i);
            finish();
        }
    }

}
